    //
//  FeaturedMemberCell.m
//  SpendFunk
//
//  Created by bm on 4/19/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "FeaturedMemberCell.h"
#import "macro.h"
#import "image.h"
#import "Global.h"

@implementation FeaturedMemberCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
        [self initCellView];
		isFollow = TRUE;
    }
    return self;
}

-(void)initCellView{
	UIView *myContentView = self.contentView;
	//int nW = myContentView.frame.size.width;
	int nH = myContentView.frame.size.height;
//img	
	NSString *path = [[NSString alloc] initWithFormat:@"%@",IMG_VIEW_CELL];
	UIImage *img = [UIImage imageNamed:path];
	m_pImgvCell = [[UIImageView alloc] initWithFrame:CGRectMake(5, nH/2-img.size.height/2, img.size.width, img.size.height)];
	[m_pImgvCell setImage:img];
	[myContentView addSubview:m_pImgvCell];
//name	
	m_pLblName = [[UILabel alloc] initWithFrame:CGRectMake(15+img.size.width, 5, 100, 30)];
	[myContentView addSubview:m_pLblName];
	[m_pLblName setBackgroundColor:[UIColor clearColor]];
	[m_pLblName release];
//arrow	
	path = [[NSString alloc] initWithFormat:@"%@",IMG_BTN_ARROW];
	img = [UIImage imageNamed:path];
	m_pBtnArrow = [[UIButton alloc] initWithFrame:CGRectMake(150, 15, img.size.width, img.size.height)];
	[m_pBtnArrow setBackgroundImage:img forState:UIControlStateNormal];
	[m_pBtnArrow setBackgroundColor:[UIColor clearColor]];
	[myContentView addSubview:m_pBtnArrow];
//follow
	path = [[NSString alloc] initWithFormat:@"%@",IMG_BTN];
	img = [UIImage imageNamed:path];
	m_pBtnFollow = [[UIButton alloc] initWithFrame:CGRectMake(320-img.size.width-40, 5, img.size.width+10, img.size.height)];
	[m_pBtnFollow setBackgroundImage:img forState:UIControlStateNormal];
	[m_pBtnFollow setBackgroundColor:[UIColor clearColor]];
	[m_pBtnFollow addTarget:nil action:@selector(onClickFollow) forControlEvents:UIControlEventTouchUpInside];
	[myContentView addSubview:m_pBtnFollow];	
}

- (void)layoutSubviews 
{
	[super layoutSubviews];
	/*
	// getting the cell size
	CGRect contentRect = self.contentView.bounds;
	CGFloat boundsX = contentRect.origin.x;
	CGRect frame;
	
	CGFloat leftMargin = 40.f, nDelta = 10;
	*/
}

-(void)setLabelName:(NSString *)name{
	m_pLblName.text = name;
}

-(void)setUserId:(NSString*)userId{
	m_pUserId = [userId retain];
}

-(void)setFollower:(BOOL)isFollower{
	isFollow = isFollower;
	if(isFollow)
	{
		[m_pBtnFollow setBackgroundImage:[UIImage imageNamed:IMG_BTN] forState:UIControlStateNormal];
		[m_pBtnFollow setTitle:@"Follow" forState:UIControlStateNormal];
	}
	else if(!isFollow)
	{
		[m_pBtnFollow setBackgroundImage:[UIImage imageNamed:IMG_BTN_CLICK] forState:UIControlStateNormal];
		[m_pBtnFollow setTitle:@"Following" forState:UIControlStateNormal];
	}
}


-(void)onClickFollow{
	isFollow = !isFollow;
	if(!isFollow){
		[m_pBtnFollow setBackgroundImage:[UIImage imageNamed:IMG_BTN_CLICK] forState:UIControlStateNormal];
		[m_pBtnFollow setTitle:@"Following" forState:UIControlStateNormal];
	}else{
		[m_pBtnFollow setBackgroundImage:[UIImage imageNamed:IMG_BTN] forState:UIControlStateNormal];
		[m_pBtnFollow setTitle:@"Follow" forState:UIControlStateNormal];
	}
	[self setFollower];
}


-(void)setFollower{
	NSString *post = nil;
	NSData *postData = nil;
	NSString *postLength = nil; 
	NSURL *url = nil;
	if(isFollow)
	{
		post = [NSString stringWithFormat:@"userid_fr=%@&userid_to=%@", g_strLoginUserId, m_pUserId];
		postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];  
		postLength = [NSString stringWithFormat:@"%d", [postData length]];
		url = [NSURL URLWithString:@"http://demo.thejupitech.com/snt/spendfunk/api/follow/setfollow"];
	}
	else if(!isFollow)
	{
		post = [NSString stringWithFormat:@"userid_fr=%@&userid_to=%@", g_strLoginUserId, m_pUserId];
		postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];  
		postLength = [NSString stringWithFormat:@"%d", [postData length]];
		url = [NSURL URLWithString:@"http://demo.thejupitech.com/snt/spendfunk/api/follow/unsetfollow"];
	}
	
	NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
	[theRequest setHTTPMethod:@"POST"];  
	[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];  
	[theRequest setHTTPBody:postData];	
	
	NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
	if( theConnection )
	{
		m_pWebData = [[NSMutableData data] retain];
		isContinueParse = true;
	}
	else
	{
		
	}
}


#pragma mark delegate connection
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
	[m_pWebData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
	[m_pWebData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Cannot connect to internet" message:@"Please connect to internet!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
	[alert show];
	
	[alert release];
	[connection release];
	[m_pWebData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
	NSData *loginStatus = [[NSData alloc] initWithBytes: [m_pWebData mutableBytes] length:[m_pWebData length]];
	
	//NSString *a = [[NSString alloc] initWithData:loginStatus encoding:NSUTF8StringEncoding];
	//NSLog(@"%@",a);
	
	NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:loginStatus];
	[xmlParser setDelegate:self];
	
	BOOL success = [xmlParser parse];
	
	if(success)
	{
		NSLog(@"No Errors");
		
	}
	else
	{
		NSLog(@"Error Error Error!!!");
	}
	
	[connection release];
	[m_pWebData release];
}

#pragma mark delegate xmlParser
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
	attributes:(NSDictionary *)attributeDict {
	if(!currentElement) 
		currentElement = [[NSString alloc] initWithString:elementName];
	else
		currentElement = [elementName retain];
	
	NSLog(@"Processing Element: %@", elementName);
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
	NSLog(@"Processing Value: %@", string);
	
	if([currentElement isEqualToString:@"status"])
	{
		if([currentElementValue isEqualToString:@"false"])
		{
			
		}
		else
		{
			
		}
	}
	else if([currentElement isEqualToString:@"info"])
	{
	 
	}
	
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
	
	[currentElement release];
	currentElement = nil;
	[currentElementValue release];
	currentElementValue = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
